package com.kldy8.kldyw;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.kldy8.kldyw.filmfactory.FilmTalent.FilmTalentActivity;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.web.AppWebUrl;
import com.kldy8.kldyw.filmfactory.company.CompanyActivity;
import com.kldy8.kldyw.filmfactory.dteam.DteamListActivity;
import com.kldy8.kldyw.filmfactory.equipment.DeviceActivity;
import com.kldy8.kldyw.filmfactory.shootingvenue.ShootingVenueActivity;
import android.support.v7.widget.Toolbar;

/**
 * Created by Administrator on 2016/7/18.
 * 首页
 */
public class FilmFactoryFragment extends Fragment
{
    private WebView webView;
    private String urlString;
    private String news_id;
    private Main2Activity activity;

    private RelativeLayout reloadlayout;
    private RelativeLayout loadinglayout;
    private ImageView reload_img;

    public static FilmFactoryFragment newInstance(String param1)
    {
        FilmFactoryFragment fragment = new FilmFactoryFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    public FilmFactoryFragment()
    {

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_film_factory, container, false);

        activity = (Main2Activity) getActivity();
        urlString = AppWebUrl.createUrl(AppWebUrl.URL_MAKE);

        init(view,urlString);
        return view;
    }

    private void init(View view,final String urlString)
    {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.make));//设置主标题

        loadinglayout = (RelativeLayout) view.findViewById(R.id.loadinglayout);
        reloadlayout = (RelativeLayout) view.findViewById(R.id.reloadlayout);
        reload_img = (ImageView) view.findViewById(R.id.reload_img);
        reload_img.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                loadinglayout.setVisibility(View.VISIBLE);
                webView.loadUrl(urlString);
            }
        });



        webView = (WebView) view.findViewById(R.id.webView);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //WebView加载web资源
        webView.loadUrl(urlString);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl)
            {
                // TODO Auto-generated method stub
//                super.onReceivedError(view, errorCode, description, failingUrl);
                loadinglayout.setVisibility(View.GONE);
                reloadlayout.setVisibility(View.VISIBLE);
            }
            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                loadinglayout.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                reloadlayout.setVisibility(View.GONE);
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                Log.e("this is my Url",url);

                if (url.contains("10086"))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (!url.contains("kldy")&&(!url.contains("http://192.168.1.200")))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/job/person_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, FilmTalentActivity.class);
                    intent.putExtra("item",0);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/job/company_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, FilmTalentActivity.class);
                    intent.putExtra("item",1);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/dteam/dteam_list"))
                {
                    Intent intent = new Intent();
                    intent.putExtra("item",0);
                    intent.setClass(activity, DteamListActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/place/place_list"))
                {
                    Intent intent = new Intent();
                    if (url.contains("1"))
                    {
                        intent.putExtra("item",1);
                    }
                    else
                    {
                        intent.putExtra("item",0);
                    }
                    intent.setClass(activity, ShootingVenueActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }

                if (url.contains(UrlBuilder.URL_BASE_API+"/device/device_list"))
                {
                    Intent intent = new Intent();
                    if (url.contains("1"))
                    {
                         intent.putExtra("item",2);
                    }
                    else if (url.contains("0"))
                    {
                        intent.putExtra("item",1);
                    }
                    else
                    {
                        intent.putExtra("item",0);
                    }
                    intent.setClass(activity, DeviceActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/company/company_list"))
                {
                    String type = "15";
                    if (url.contains("type_id=15"))
                    {
                        type = "15";
                    }
                    if (url.contains("type_id=14"))
                    {
                        type = "14";
                    }
                    if (url.contains("type_id=16"))
                    {
                        type = "16";
                    }
                    if (url.contains("type_id=17"))
                    {
                        type = "17";
                    }
                    Intent intent = new Intent();
                    intent.putExtra("type_id",type);
                    intent.setClass(activity, CompanyActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                else
                {
                    Intent intent = new Intent();
                    String resultUrl = AppWebUrl.createUrl(url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass(activity, MyWebActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
            }
        });
    }
}
